<template>
  <div>
    <!-- //面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addGoodsClassFn">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <!-- <el-table :data="goodsClassList">
        <el-table-column label="分类名" prop="cat_name"></el-table-column>
        <el-table-column label="级别" prop="cat_level"></el-table-column>
      </el-table>-->
      <zk-table
        :data="goodsClassList"
        :columns="cols"
        :expand-type="false"
        :selection-type="false"
        :show-index="true"
        index-text="#"
        border
      >
        <!-- 是否生效模板 -->
        <template #isOk="scopedObj">
          <i class="el-icon-success" style="color:'red'" v-if="scopedObj.row.cat_delete"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序模板 -->
        <template #sort="scopedObj">
          <el-tag v-if="scopedObj.row.cat_level == 0">一级</el-tag>
          <el-tag v-else-if="scopedObj.row.cat_level == 1">二级</el-tag>
          <el-tag v-else-if="scopedObj.row.cat_level == 2">三级</el-tag>
        </template>
      </zk-table>
      <!-- 操作模板 -->
      <template v-slot:doing>
        <el-button type="warning" plain>警告按钮</el-button>
        <el-button type="warning" plain>警告按钮</el-button>
      </template>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[1, 3, 5, 8]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 新增商品分类弹出框 -->
    <el-dialog title="提示" :visible.sync="addClassIsShow" width="30%">
      <!-- 新增表单 -->
      <el-form
        :model="addClassFrom"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        @close='cloceFn'
      >
        <!-- :rules校验规则绑定相应数据对象 -->
        <!-- :model 表单内的值和data数据双向绑定 -->
        <el-form-item label="分类名称">
          <el-input v-model="addClassFrom.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类">
          <!-- 级联选择器 -->
          <el-cascader
          :options="parentTwoClassList"
          :props="configObj"
          v-model="targetId"
          @change="valChangeFn"
          >
           </el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addClassIsShow = false">取 消</el-button>
        <el-button type="primary" @click="sureAddGoodsClassFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsClassInfoAPI,addGoodsClassAPI } from '@/api'
export default {
  data() {
    return {
      queryData: {
        type: 3,
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5
      },
      total: -1,
      //新增商品分类弹出框是否显示
      addClassIsShow: false,
      goodsClassList: [],
      // 级联选择器配置对象
      configObj:{
        value:'cat_id',
        label:'cat_name',
        children:'children'
      },
      //级联选择器选中的值(与级联选择器双向数据绑定)需是数组
      targetId:[],
      //新增商品分类父级二级分类数据源
      parentTwoClassList:[],
      // 收集新增分类弹出框表单信息
      addClassFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      //新增商品分类表单验证规则
      addClassFromRules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      cols: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '200px'
        },
        {
          label: '是否生效',
          width: '200px',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          width: '200px',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          width: '200px',
          type: 'template',
          template: 'doing'
        }
      ]
    }
  },
  methods: {
    //分页总容量改变事件
    handleSizeChange(newSize) {
      this.queryData.pagesize = newSize
      this.getGoodsClassList()
    },
    //分页页码值改变事件
    handleCurrentChange(newPage) {
      this.queryData.pagenum = newPage
      this.getGoodsClassList()
    },
    //局部刷新页面处理函数
    async getGoodsClassList() {
      const { data } = await getGoodsClassInfoAPI(this.queryData)
      //如果获取失败则退出
      if (data.meta.status != 200) return this.msg.error('获取失败')
      this.goodsClassList = data.data.result
      this.total = data.data.total
      console.log(this.goodsClassList)
    },
    //新增商品分类
    addGoodsClassFn() {
      this.addClassIsShow = true
      this.getParenttwo()
    },
    //确定新增商品分类
    async sureAddGoodsClassFn() {
      this.addClassIsShow = false
      const {data} = await addGoodsClassAPI(this.addClassFrom)
      this.msg.warning(data.meta.msg)
      this.getGoodsClassList()
    },
    //新增商品分类父级二级分类数据源
    async getParenttwo(){
      const {data} = await getGoodsClassInfoAPI({type:2})
      this.parentTwoClassList = data.data
    },
    //级联选择器内的值发生改变时触发事件
    valChangeFn(){
      //targetId数组内存放着几级选中商品id的集合
      this.addClassFrom.cat_pid = this.targetId[1]
      this.addClassFrom.cat_level = this.targetId.length
    },
    //新增商品分类表单关闭事件
    cloceFn(){
      alert(11)
    }
  },
  created() {
    this.getGoodsClassList()
  }
}
</script>

<style>
</style>